package com.wfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfx.entity.*;
import com.wfx.mapper.WxbGoodsBrandMapper;
import com.wfx.service.IWxbGoodsBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2021-01-20
 */
@Service
public class WxbGoodsBrandServiceImpl extends ServiceImpl<WxbGoodsBrandMapper,WxbGoodsBrand> implements IWxbGoodsBrandService {

    @Autowired
    private WxbGoodsBrandMapper wxbGoodsBrandMapper;
    //查询
    @Override
    public PageResult<WxbGoodsBrand> search(PageDTO<WxbGoodsBrand> pageDTO) {
        if (pageDTO == null) {
            return new PageResult<WxbGoodsBrand>(false,"查询失败",null,null,null);
        }
        IPage<WxbGoodsBrand> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());

        WxbGoodsBrand data = pageDTO.getData();

        QueryWrapper<WxbGoodsBrand> queryWrapper = new QueryWrapper<>(pageDTO.getData());
        if (!StringUtils.isEmpty(data.getName())){
            queryWrapper.eq("name",data.getName());
        }
        IPage<WxbGoodsBrand> pageInfo = wxbGoodsBrandMapper.selectPage(iPage,queryWrapper);

        PageResult pageResult = new PageResult<>();
        pageResult.setSuccess(true);
        pageResult.setData(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setMsg("查询成功");
        return pageResult;
    }
    //添加or修改
    @Override
    public Result addOrUpdate(WxbGoodsBrand wxbGoodsBrand) {

        if (wxbGoodsBrand == null) {
            return new Result(false,"参数异常");
        }
        if(!StringUtils.isEmpty(wxbGoodsBrand.getId())){
            int i = wxbGoodsBrandMapper.updateById(wxbGoodsBrand);
            return  new Result(i==1?true:false,i==1?"修改成功":"修改失败");
        }else {
            int i = wxbGoodsBrandMapper.insert(wxbGoodsBrand);
            return  new Result(i==1?true:false,i==1?"添加成功":"添加失败");
        }
    }
    //批量删除
    @Override
    public Result delBrands(List<String> ids) {
        if (ids == null) {
            return new Result(false,"参数异常");
        }
        int i = wxbGoodsBrandMapper.deleteBatchIds(ids);
        return  new Result(i>0?true:false,i>0?"删除成功":"删除失败");
    }
    //删除
    @Override
    public Result delOne(WxbGoodsBrand wxbGoodsBrand) {
        if (wxbGoodsBrand == null) {
            return new Result(false,"参数异常");
        }
        int i = wxbGoodsBrandMapper.deleteById(wxbGoodsBrand.getId());
        return  new Result(i>0?true:false,i>0?"删除成功":"删除失败");
    }

    @Override
    public List<WxbGoodsBrand> select(List<String> brandsList) {
        List<WxbGoodsBrand> wxbGoodsBrands = new ArrayList<>();
        for (String s : brandsList) {
            WxbGoodsBrand wxbGoodsBrand = wxbGoodsBrandMapper.selectById(s);
            wxbGoodsBrands.add(wxbGoodsBrand);
        }
        return wxbGoodsBrands;
    }
}
